package study.javaee.practice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import study.javaee.practice.dao.ProjectMemberDao;
import study.javaee.practice.entity.ProjectMember;
import study.javaee.practice.entity.vo.MemberVO;
import study.javaee.practice.entity.vo.ProjectJoinApplyVO;
import study.javaee.practice.service.ProjectMemberService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProjectMemberServiceImpl implements ProjectMemberService {
    @Resource
    private ProjectMemberDao projectMemberDao;
    @Override
    public Boolean insertProjectMember(ProjectJoinApplyVO data) {

        ProjectMember projectMember=new ProjectMember();
        int project_Id= data.getId();
        int user_Id= data.getUserId();
        int State=data.getState();

        //根据从前端返回的ProjectMember查询数据库中是否已经存在该ProjectMember
        QueryWrapper<ProjectMember> wrapper = new QueryWrapper<ProjectMember>();
        Map<String , Object> map = new HashMap<>();
        map.put("project_id" , project_Id);
        map.put("user_id" , user_Id);
        wrapper.allEq(map);
        int count = projectMemberDao.selectCount(wrapper);

        projectMember.setProjectId(project_Id);
        projectMember.setUserId(user_Id);
        //点击通过申请
        if(State==1){
            if(count==0){
                //当数据库不存在该数据时才进行插入
                projectMemberDao.insert(projectMember);
            }
            return true;
        }
        //点击过通过但后面又点击未通过的情况
        //应删除原来插入的数据
        else if(State==0&&count!=0){
            wrapper.allEq(map);
            projectMemberDao.delete(wrapper);
            return true;
        }
        else {
            return false;
        }
    }
    @Override
    public List<MemberVO> findAllMember(MemberVO data){
        Integer id=data.getId();
        String name= data.getName();
        String username= data.getUsername();
        if(id==null) {
            List<MemberVO> MemberVOS = projectMemberDao.getAllMemberVo();
            return MemberVOS;
        }
        else {
            if(id!=null){
                List<MemberVO> MemberVOS = projectMemberDao.getMemberVoById(id);
                return MemberVOS;
            }
            else {
                return null;
            }
        }
    }
}
